<?php
class AdminTourController extends MyZend_Controller_Action {
	protected $_arrParam;
	
	protected $_actionMain;
	
	protected $_currentController;
	
	protected $__namespace;
	
	protected $_paginator = array(
			'itemCountPerPage' => 5,
			'pageRange' => 4
	);
	
	//protected $_errors = array();
	
	public function init() {
	
		$this->_arrParam          = $this->_request->getParams();
		$this->_currentController = '/' . $this->_arrParam['module']
		. '/' . $this->_arrParam['controller'];
	
		$this->_actionMain        = '/' . $this->_arrParam['module']
		. '/' . $this->_arrParam['controller'] . '/index';
	
		// filter session
		$this->_namespace         = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
	
		if(empty($ssFilter->col)){
			$ssFilter->keywords = '';
			$ssFilter->col      = 't.id';
			$ssFilter->order    = 'DESC';
		}
	
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		$this->_arrParam['ssFilter']['col'] 		= $ssFilter->col;
		$this->_arrParam['ssFilter']['order'] 		= $ssFilter->order;
	
		// paginator
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator']    = $this->_paginator;
	
		// render to view
		$this->view->arrParam 		   = $this->_arrParam;
		$this->view->actionMain 	   = $this->_actionMain;
		$this->view->currentController = $this->_currentController;
		$this->view->fileUrl           = FILES_URL;
		// select box array location
	
		$this->_helper->layout()->disableLayout();
		$template_path = TEMPLATE_PATH . '/admin/main';
		$this->loadTemplate($template_path);
	}
	
	public function indexAction() {
		$this->view->Title = 'Tour :: manager :: List';
		
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		if(isset($ssFilter->timetable)){
			unset($ssFilter->timetable);
		}
		if(isset($ssFilter->notice)){
			unset($ssFilter->notice);
		}
		$this->view->headTitle($this->view->Title, true);
		$tblTour = new Default_Model_Tour();
		$this->view->Items = $tblTour->listItem($this->_arrParam, array('task' => 'admin-list'));
	
		$paginator = new MyZend_Paginator();
		$totalItem = $tblTour->countItem($this->_arrParam);
		$this->view->paginator = $paginator->creatPaginator($totalItem, $this->_paginator);
			
	}
	
	// filter data post
	public function filterAction(){
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
	
		// sort
		if($this->_arrParam['type'] == 'order') {
			$ssFilter->col   = $this->_arrParam['col'];
			$ssFilter->order = $this->_arrParam['by'];
		}
	
		// seach
		if($this->_arrParam['type'] == 'search'){
			if($this->_arrParam['key'] == 1){
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
			}else{
				$ssFilter->keywords = '';
			}
		}
		$this->_redirect($this->_actionMain);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	// change status
	public function statusAction() {
		$tblTour = new Default_Model_Tour();
		$tblTour->changeStatus($this->_arrParam);
		$this->_redirect($this->_actionMain);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	// order items
	public function sortAction() {
		if($this->_request->isPost()){
			$cid   = $this->_arrParam['cid'];
			$order = $this->_arrParam['order'];
	
			if(count($cid) > 0) {
				$validator = new Zend_Validate();
				$validator->addValidator(new Zend_Validate_NotEmpty(), true);
				$validator->addValidator(new Zend_Validate_Int(), true);
				$validator->addValidator(new Zend_Validate_GreaterThan(0), true);
	
				foreach ($cid as $key => $value) {
					if(!$validator->isValid($order[$value])) {
						$messages        = $validator->getMessages();
						// unset value order not validator
						unset($this->_arrParam['cid'][$key]);
					}
				}
	
	
				$tblTour = new Default_Model_Tour();
				$tblTour->sortItems($this->_arrParam);
			}
	
			$this->_redirect($this->_actionMain);
		}
	}
	
	// delete item
	public function deleteAction() {
		$this->view->Title = 'Tour :: Manager :: Delete';
		$this->view->headTitle($this->view->Title,true);
		if($this->_request->isPost()){
			$tblTour = new Default_Model_Tour();
			$tblTour->deleteItem($this->_arrParam, array('task'=>'admin-delete'));
			
			$upload = new MyZend_File_Upload();
			$file_dir  = FILES_PATH . '/tour_picture/large/' . $this->_arrParam['id'] . '_tour.jpg';
			$upload->removeFile($file_dir);
			$this->_redirect($this->_actionMain);
		}
	}
	
	// delete multi items
	public function multiDeleteAction() {
		if($this->_request->isPost()){
			$tblTour = new Default_Model_Tour();
			$tblTour->deleteItem($this->_arrParam, array('task'=>'admin-multi-delete'));
			
			$upload = new MyZend_File_Upload();
			$cid    = $this->_arrParam['cid'];
			foreach ($cid as $key => $value) {
				$file_dir  = FILES_PATH . '/tour_picture/large/' . $value . '_tour.jpg';
				$upload->removeFile($file_dir);
			}
			$this->_redirect($this->_actionMain);
		}
	}
	
	// add new item
	public function addAction() {
		$this->view->Title = 'Tour :: Manager :: Add New';
		$this->view->headTitle($this->view->Title,true);
		$tblCategory = new Default_Model_TourCategory();
		$this->view->categorySelect = $tblCategory->itemInSelectbox();
		
		$tblContact  = new Default_Model_Contact();
		$this->view->contactSelect  = $tblContact->itemInSelectbox();
		
		$this->view->idTimetable   = 0;
		$this->view->idNotice      = 0;
	    
		if($this->_request->isPost()) {
		    $tblTour   = new Default_Model_Tour();   
		    $ssFilter  = new Zend_Session_Namespace($this->_namespace);
			$validator = new Default_Form_ValidateTour($this->_arrParam);
			if($validator->isError()) {
			 	$id_timetable = count($ssFilter->timetable) - 1;
				$id_notice    = count($ssFilter->notice) - 1;
				$this->view->messageError = $validator->getMessageError();
				$data                     = $validator->getData();
				
				$data['time_table_title'] = $ssFilter->timetable[$id_timetable]['title'];
				$data['time_table']       = $ssFilter->timetable[$id_timetable]['content'];
				
				$data['notice_title']     = $ssFilter->notice[$id_notice]['title'];
				$data['notice']           = $ssFilter->notice[$id_notice]['content'];
				
				$this->view->idTimetable  = $id_timetable - 1;
				$this->view->idNotice     = $id_notice - 1;
		        $this->view->data         = $data;
			}else { 
				$this->_arrParam['timetable']  = serialize($ssFilter->timetable);
				$this->_arrParam['notice']     = serialize($ssFilter->notice);
				
			    $id = $tblTour->saveItem($this->_arrParam, array('task'=>'admin-add'));
				
				$upload      = new MyZend_File_Upload();
				$upload_dir  = FILES_PATH . '/tour_picture';
				$imageName   = $upload->upload('picture_tour', $upload_dir, array('task' => 'rename'), 'tour', 'jpg');
				$image       = new MyZend_File_Images();
				$file_dir    = FILES_PATH . '/tour_picture/' . $imageName;
				$upload_dir  = FILES_PATH . '/tour_picture/large/' . $id . '_' . $imageName;
				$image->creatImage($file_dir, 264, 191, $upload_dir);
				$upload->removeFile($file_dir);
				
				$this->_arrParam['id']    = $id;
				$this->_arrParam['image'] = $id . '_' . $imageName;
				$tblTour->saveItem($this->_arrParam, array('task'=>'admin-add-image')); 
				
				$this->_redirect($this->_actionMain);
			} 
	
		}
	}
	
	// change time table 
	public function editorAction() {
		 $this->_helper->layout()->disableLayout();
		 $ssFilter         = new Zend_Session_Namespace($this->_namespace);
		 $type             = $this->_arrParam['type'];
		 $this->view->type = $type;
		 
		 
		 if($this->_arrParam['task'] == 'go') {
			 $validator = new Default_Form_ValidateComentTour($this->_arrParam);
			 if($validator->isError()) {
			 	 $this->view->messageError = $validator->getMessageError();
			 	 $this->view->data         = $validator->getData();
			 	 $this->view->id           = $this->_arrParam['id'];
			 }else {
				 if(empty($ssFilter->$type)){
					   $tmp = array();
					   $tmp[] = array('title' => $this->_arrParam['title'], 'content' => $this->_arrParam['content']);
					   $this->view->id = $this->_arrParam['id'];
					   $ssFilter->$type = $tmp;
				 }else {
				 	   $tmp =  $ssFilter->$type;
					   if($this->_arrParam['id'] == (count($ssFilter->$type) - 1)) {
							$tmp[] = array('title' => $this->_arrParam['title'], 'content' => $this->_arrParam['content']);
							$ssFilter->$type  = $tmp;
							$this->view->id = $this->_arrParam['id'] + 1;
					   }else {
						    $tmp[$this->_arrParam['id'] + 1] = array('title' => $this->_arrParam['title'], 'content' => $this->_arrParam['content']);
						 	$this->view->data = $tmp[$this->_arrParam['id'] + 2];
						 	$ssFilter->$type  = $tmp;
						 	$this->view->id   = $this->_arrParam['id'] + 1;
					   }
					 
				}
						
			}
		 }
		 
		 
		 if($this->_arrParam['task'] == 'back') {
		 	if(!empty($ssFilter->$type)){
		 		$tmp =  $ssFilter->$type;
			 	if($this->_arrParam['id'] >= 0) {
			 		$this->view->id   = $this->_arrParam['id'] - 1;
			        $this->view->data = $tmp[$this->_arrParam['id']];
			 	}else{
			 		$this->view->data = $tmp[0];
			 		$this->view->id = $this->_arrParam['id'];
			 	}
		 	}
		 	
		 }
		 
		 if($this->_arrParam['task'] == 'delete') {
		     if(!empty($ssFilter->$type)){
		     	 $tmp =  $ssFilter->$type;
			     if(isset($tmp[$this->_arrParam['id'] + 1])) {
			     	unset($tmp[$this->_arrParam['id'] + 1]);
			     	$tmp = array_values($tmp);
			     	$ssFilter->$type  = $tmp;
			     	$this->view->data = $tmp[$this->_arrParam['id'] + 1];
			     	$this->view->id   = $this->_arrParam['id'];
			     }	
		     }
		 }		 
	   $this->view->editor =  $ssFilter->$type;
	}
	
	// view detail item
	public function infoAction() {
		$this->view->Title = 'Tour ::  Manager :: Edit';
		$this->view->headTitle($this->view->Title,true);
		$tblTour = new Default_Model_Tour();     
		
		$tblCategory = new Default_Model_TourCategory();
		$this->view->categorySelect = $tblCategory->itemInSelectbox();
		
		$tblcontact  = new Default_Model_Contact();
		$this->view->contactSelect = $tblcontact->itemInSelectbox();
		
		$this->view->Item = $tblTour->getItem($this->_arrParam, array('task' => 'admin-info'));
   }
	
	// edit item
	public function editAction() {
		$ssFilter  = new Zend_Session_Namespace($this->_namespace);
		$this->view->Title = 'Tour ::  Manager :: Edit';
		$this->view->headTitle($this->view->Title,true);
		$tblTour = new Default_Model_Tour();     
		
		$tblCategory = new Default_Model_TourCategory();
		$this->view->categorySelect = $tblCategory->itemInSelectbox();
		
		$tblContact  = new Default_Model_Contact();
		$this->view->contactSelect  = $tblContact->itemInSelectbox();
		
		$item = $tblTour->getItem($this->_arrParam, array('task' => 'admin-edit'));
		
		if(!isset($ssFilter->timetable)){
			$ssFilter->timetable = unserialize($item['timetable']);
		}
		if(!isset($ssFilter->notice)){
			$ssFilter->notice = unserialize($item['notice']);
		}
		
		$data['time_table_title'] = $ssFilter->timetable[0]['title'];
		$data['time_table']       = $ssFilter->timetable[0]['content'];
				
		$data['notice_title']     = $ssFilter->notice[0]['title'];
		$data['notice']           = $ssFilter->notice[0]['content'];
		$this->view->data         = $data;
		
		$this->view->Item         = $item;
		$this->view->idTimetable  = -1;
		$this->view->idNotice     = -1;
		
		if($this->_request->isPost()) {
			$validator = new Default_Form_ValidateTour($this->_arrParam);
			if($validator->isError()) {
			    $id_timetable = count($ssFilter->timetable) - 1;
				$id_notice    = count($ssFilter->notice) - 1; 
				$this->view->messageError = $validator->getMessageError();
				$data                     = $validator->getData();
				
				$data['image']            = $item['image'];
		        $this->view->Item         = $data;
			}else {
				$this->_arrParam['timetable']  = serialize($ssFilter->timetable);
				$this->_arrParam['notice']     = serialize($ssFilter->notice);
			    $tblTour->saveItem($this->_arrParam, array('task'=>'admin-edit'));
				if($validator->getImage() == true) {
					$id          = $this->_arrParam['id'];
					$upload      = new MyZend_File_Upload();
					$upload_dir  = FILES_PATH . '/tour_picture';
					$imageName   = $upload->upload('picture_tour', $upload_dir, array('task' => 'rename'), 'tour', 'jpg');
					$image       = new MyZend_File_Images();
					$file_dir    = FILES_PATH . '/tour_picture/' . $imageName;
					$upload_dir  = FILES_PATH . '/tour_picture/large/' . $id . '_' . $imageName;
					$image->creatImage($file_dir, 264, 191, $upload_dir);
					$upload->removeFile($file_dir);
					
					$this->_arrParam['id']    = $id;
					$this->_arrParam['image'] = $id . '_' . $imageName;
					$tblTour->saveItem($this->_arrParam, array('task'=>'admin-add-image'));
				}
				$this->_redirect($this->_actionMain);
			}
		}
	}
}